package com.kingbi.system.user.jsfBean;

import java.sql.SQLException;
import java.util.List;
import javax.faces.component.UIData;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import javax.servlet.http.HttpSession;
import org.apache.log4j.Logger;
import com.kingbi.system.user.service.UserService;
import com.kingbi.util.ConstantFiles.UserConstant;

/**
 * 用户类
 *
 */
public class UserJsfBean extends UserDataBean {

	static Logger logger = Logger.getLogger(UserJsfBean.class);
	public UserService userService;
	
	public List<UserDataBean> userDataList = null; // --数据
	public UIData userDataListUI; //--绑定属性

	
	/**
	 * 用户查询方法
	 */
    public String searchUser(){
    	userDataListUI = null;
        selected = null;
    	logger.warn("**********************searchUser()");
    	HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
    	String s_new_CurrentPage = (String) FacesContext.getCurrentInstance().
    						getExternalContext().getRequestParameterMap().get("newCurrentPage");
		String s_query_flag = (String) FacesContext.getCurrentInstance()
							.getExternalContext().getRequestParameterMap().get("queryFlag");//--翻页标志
		
		String s_funRight = (String) FacesContext.getCurrentInstance()
							.getExternalContext().getRequestParameterMap().get("funRight");//--功能管理、浏览权限标识
		
		String s_searchFlag = (String) FacesContext.getCurrentInstance().getExternalContext().
							getRequestParameterMap().get("searchFlag"); //--列表页面提交查询标识
		String curUserId = (String) session.getAttribute(UserConstant.USER_MAP_KEY_ID);
		
		if (s_funRight != null && !s_funRight.equals("") && !s_funRight.equals("null")) {
			setMagFlag(s_funRight);
		}

		if (s_query_flag != null && s_query_flag.equals("ispage")) {
			listUpdateFlag = false;
		} else {
			listUpdateFlag = true;
			if (!(s_searchFlag != null && s_searchFlag.equals("isListSearch"))) {
				getEmptyJsfBean();
			}
		}
		try {
			UserDataBean userDataBean = new UserDataBean();
			getJsfDataModel(userDataBean);
	        if (listUpdateFlag) {  //--总记录数
				setRecordCount(userService.searchUserCount(userDataBean));
			}
	
			if (s_new_CurrentPage != null && !s_new_CurrentPage.equals("") && !s_new_CurrentPage.equals("null")) {//--当前页号
				if (!(s_new_CurrentPage.equals("newPage"))) {
					setCurrentPage(Integer.parseInt(s_new_CurrentPage));
				}
			} else {
				setCurrentPage(1);
			}
	        dealPage(getRecordCount(), getPageRecord(), getCurrentPage()); //--处理分页
			setStartRecordNum(((getCurrentPage() - 1)* getPageRecord() + 1)<0?0:(getCurrentPage() - 1)* getPageRecord() + 1);
            setEndRecordNum(getPageRecord()*getCurrentPage());
            setUserDataList(userService.searchUserData(userDataBean, (getCurrentPage() - 1)* getPageRecord() + 1, getPageRecord()));
		} catch (Exception e) {
			e.printStackTrace();
			return "failure";
		}
		return "viewList";
    }
    
    
    /**
	 * 导向添加页面
     * @throws SQLException 
	 */
	public String preAdd() throws SQLException {
		getEmptyJsfBean();
		this.getRoleIdList().clear();
		logger.warn("**********************perAdd()");
		
		List<UserDataBean> objList = userService.getRoleInfo();
		for(UserDataBean user: objList){
			this.getRoleIdList().add(new SelectItem(user.getRoleCode(),user.getRoleName()));
		}
		return "addUser";
	}
	
	/**
	 * 提交添加或修改操作
	 */  
	public String saveOrupdate() {
		logger.warn("**********************saveOrupdate");
		List<String> roleIds = this.getRoleIds();
		System.out.println(roleIds);
		return "saveOrupdate";
	}

	public List<UserDataBean> getUserDataList() {
		return userDataList;
	}

	public void setUserDataList(List<UserDataBean> userDataList) {
		this.userDataList = userDataList;
	}

	public UIData getUserDataListUI() {
		return userDataListUI;
	}

	public void setUserDataListUI(UIData userDataListUI) {
		this.userDataListUI = userDataListUI;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
		searchUser();
	}
}
